<?php

defined('BASEPATH') OR exit('No direct script access allowed');


class wordpress{

    public $sitelink;
    public $checklink=false;
    private $url;
    private $key="mmdreamisok";
    private $moq="";

    public function __construct($param=array())
    {
        $this->sitelink=$param["domian"];
        $this->key=$param["apipass"];
        $this->url=$this->sitelink;
        $this->checklink();
    }

    private function checklink()
    {
        
        $reuslt=$this->sendrequest();
        if($reuslt=="dreamisok")
        {
            $this->checklink=true;
        }
    }


    public function synccate()
    {
        $param["action"]="category";
        $result=$this->sendrequest($param);
        $result = json_decode($result,true);
        return $result;

    }

    public function syncenquiry($page,$maxid)
    {
        $param["action"]="sync_enquiry";
        $param["page"]=$page;
        $param["maxid"]=$maxid;
        $result=$this->sendrequest($param);
        var_dump($result);
        $result = json_decode($result,true);
        return $result;
    }
    
    private function formatdesc($html)
    {
        $newhtml = str_replace("\n","",$html);
        $a = preg_match_all("#<td class=\"prop-name\">(.*?):\s+</td>\s+<td class=\"prop-value\">(.*?)</td>#is",$newhtml,$mc);
        if($a)
        {
            //print_r($mc);
            $skipword = ["Brand","Port","Payment Terms"];
            $html = '<table class="prop-list">';
            foreach($mc[1] as $k=>$v)
            {
                $val = $mc[2][$k];
                $skip = 0;
                foreach($skipword as $wd)
                {
                    //echo $v.":".$wd."--";
                    if(stristr($v,$wd))
                    {
                        $skip = 1;
                        break;
                    }
                }
                if(strtolower($v)=="moq")
                {
                    $this->moq=trim(str_ireplace("pcs","",$val));
                }
                if($skip==1)
                {
                    continue;
                }
                if(strstr($v,"Origin"))
                {
                    $val = "China";
                }
                if(strstr($val,"http"))
                {
                    continue;
                }
                $html .= '<tr><td class="prop-name">'.trim($v).':</td><td class="prop-value">'.trim($val).'</td></tr>';
            }
            $html.= '</table>';
        }
        return $html;
    }

    public function addproduct($proinfo)
    {
        $proinfo["desc"] = $this->formatdesc($proinfo["desc"]);
        if(empty($proinfo["moq"]) && $this->moq)
        {
            $proinfo["moq"]=$this->moq;
        }

        $param["product"]=$proinfo;
        $param["action"]="addproduct";
        $result=$this->sendrequest($param);
        $result = json_decode($result,true);
        return $result;
    }

    public function delproduct($product)
    {
        $param["postid"]=$product["rid"];
        $param["action"]="delproduct";
        $result=$this->sendrequest($param);
        $result = json_decode($result,true);
        if($result["rs"]==1)
        {
            return "ok";
        }
        return;
    }
    
    private function sendrequest($param=[])
    {
        
        if(!isset($param["action"]))
        {
            $action="test";
        }
        else
        {
            $action=$param["action"];
            unset($param["action"]);
        }
        $url = $this->url."/?__kds_flag=".$action;
        $param["kds_password"]=$this->key;

        switch($action)
        {
            case "test":
                break;
            case "category":
            case "addproduct":
            case "delproduct":
            case "sync_enquiry":
                if(!$this->checklink)
                {
                    return "broken link";
                }
                break;
            default:
                return false;
        }
        return $this->curl($url,$param);
    }

    private function curl($url,$param)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($param));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            //print curl_error($ch);
        }
        curl_close($ch);
        return $result;
    }

}
